package jfconfig;

/**
 * Created : Mar 18, 2012
 *
 * @author pquiring
 */

import java.awt.*;
import javax.swing.*;

import javaforce.*;

public class DNSEntryDialog extends javax.swing.JDialog {

  /**
   * Creates new form DNSEntryDialog
   */
  public DNSEntryDialog(java.awt.Frame parent, boolean modal, DNSPanel.Entry entry) {
    super(parent, modal);
    initComponents();
    setPosition();
    if (entry == null) return;
    name.setText(entry.name);
    if (entry.type.equals("A")) type.setSelectedIndex(0);
    if (entry.type.equals("CNAME")) type.setSelectedIndex(1);
    if (entry.type.equals("MX")) {
      type.setSelectedIndex(2);
      metric.setEnabled(true);
    }
    if (entry.type.equals("NS")) type.setSelectedIndex(3);
    metric.setText(entry.metric);
    value.setText(entry.value);
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    name = new javax.swing.JTextField();
    jLabel2 = new javax.swing.JLabel();
    type = new javax.swing.JComboBox();
    jLabel3 = new javax.swing.JLabel();
    metric = new javax.swing.JTextField();
    jLabel4 = new javax.swing.JLabel();
    value = new javax.swing.JTextField();
    accept = new javax.swing.JButton();
    cancel = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    jLabel1.setText("Name");

    jLabel2.setText("Type");

    type.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "A", "CNAME", "MX", "NS" }));
    type.addItemListener(new java.awt.event.ItemListener() {
      public void itemStateChanged(java.awt.event.ItemEvent evt) {
        typeItemStateChanged(evt);
      }
    });

    jLabel3.setText("Metric");

    metric.setEnabled(false);

    jLabel4.setText("Value");

    value.setToolTipText("Don't forget a trailing . on domain names.");

    accept.setText("Accept");
    accept.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        acceptActionPerformed(evt);
      }
    });

    cancel.setText("Cancel");
    cancel.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        cancelActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
            .addGap(0, 0, Short.MAX_VALUE)
            .addComponent(cancel)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(accept))
          .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(jLabel3)
              .addComponent(jLabel4)
              .addComponent(jLabel2)
              .addComponent(jLabel1))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(name)
              .addComponent(type, 0, 121, Short.MAX_VALUE)
              .addComponent(value)
              .addComponent(metric))))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel1)
          .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel2)
          .addComponent(type, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel3)
          .addComponent(metric, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel4)
          .addComponent(value, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(accept)
          .addComponent(cancel))
        .addContainerGap())
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void typeItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_typeItemStateChanged
    if (type.getSelectedIndex() == 2) {
      metric.setEnabled(true);
    } else {
      metric.setEnabled(false);
      metric.setText("");
    }
  }//GEN-LAST:event_typeItemStateChanged

  private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
    dispose();
  }//GEN-LAST:event_cancelActionPerformed

  private void acceptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_acceptActionPerformed
    if (!valid()) return;
    accepted = true;
    dispose();
  }//GEN-LAST:event_acceptActionPerformed

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton accept;
  private javax.swing.JButton cancel;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel4;
  private javax.swing.JTextField metric;
  private javax.swing.JTextField name;
  private javax.swing.JComboBox type;
  private javax.swing.JTextField value;
  // End of variables declaration//GEN-END:variables

  public boolean accepted = false;

  public String getName() { return name.getText(); }
  public String getDNSType() { return (String)type.getSelectedItem(); }
  public String getMetric() { return metric.getText(); }
  public String getValue() { return value.getText(); }

  private boolean isNumberValid(JTextField tf) {
    tf.setBackground(new Color(0xff0000));
    int v = JF.atoi(tf.getText());
    if ((v<0) || (v>65535)) return false;
    tf.setBackground(new Color(0xffffff));
    return true;
  }

  private boolean valid() {
    boolean ok = true;
    //NOTE : name may be empty
    if (type.getSelectedIndex() == 2) {
      if (!isNumberValid(metric)) ok = false;
    }
    if (value.getText().length() == 0) {
      value.setBackground(new Color(0xff0000));
      ok = false;
    } else {
      value.setBackground(new Color(0xffffff));
    }
    return ok;
  }

  private void setPosition() {
    Rectangle s = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
    Dimension d = getSize();
    setLocation(s.width/2 - d.width/2, s.height/2 - (d.height/2));
  }
}
